package com.edu.zju.scs.panacea.mapper.plan;

import com.edu.zju.scs.panacea.entity.plan.Plan;
import com.edu.zju.scs.panacea.entity.query.LocQuery;
import com.edu.zju.scs.panacea.entity.query.PlanQuery;
import com.edu.zju.scs.panacea.entity.vo.LocVO;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Author: Yue Liang
 * @Date: 2019-01-03 20:53
 */
@Repository
public interface PlanMapper {
    @Select("SELECT * FROM plan WHERE id = #{id}")
    @Results(id="planMap", value={
            @Result(id=true,property="id",column="id"),
            @Result(property="name",column="name"),
            @Result(property="sellerId",column="seller_id"),
            @Result(property="industryId",column="industry_id"),
            @Result(property="type",column="type"),
            @Result(property="locIds",column="loc_ids", typeHandler = com.edu.zju.scs.panacea.utils.JsonTypeHandler.class),
            @Result(property="packageIds",column="package_ids", typeHandler = com.edu.zju.scs.panacea.utils.JsonTypeHandler.class),
            @Result(property="launchTime",column="launch_time", typeHandler = com.edu.zju.scs.panacea.utils.JsonTypeHandler.class),
            @Result(property="startDate",column="start_date"),
            @Result(property="endDate",column="end_date"),
            @Result(property="state",column="state"),
            @Result(property="remark",column="remark")})
    Plan selectPlan(Long id);


    @Select("SELECT * FROM plan")
    @ResultMap(value = "planMap")
    List<Plan> selectAllPlan();

    @Delete("DELETE FROM plan where id = #{id}")
    long deletePlan(Long id);

    @Insert("INSERT INTO plan(name, seller_id, industry_id, type, loc_ids, package_ids, launch_time, start_date, end_date, state) VALUES" +
            " (#{name}, #{sellerId}, #{industryId}, #{type}, #{locIds, typeHandler=com.edu.zju.scs.panacea.utils.JsonTypeHandler}, " +
            "#{packageIds, typeHandler=com.edu.zju.scs.panacea.utils.JsonTypeHandler}, " +
            "#{launchTime, typeHandler=com.edu.zju.scs.panacea.utils.JsonTypeHandler}, " +
            "#{startDate}, #{endDate}, 0)")
    @Options(useGeneratedKeys=true)
    long insertPlan(Plan plan);

    @Update({ "update plan set " +
            "name = #{name}, " +
            "seller_id = #{sellerId}, " +
            "industry_id = #{industryId}, " +
            "type = #{type}, " +
            "loc_ids = #{locIds, typeHandler=com.edu.zju.scs.panacea.utils.JsonTypeHandler}, " +
            "package_ids = #{packageIds, typeHandler=com.edu.zju.scs.panacea.utils.JsonTypeHandler}, " +
            "launch_time = #{launchTime, typeHandler=com.edu.zju.scs.panacea.utils.JsonTypeHandler}, " +
            "start_date = #{startDate}, " +
            "end_date = #{endDate}, " +
            "state = #{state} " +
            "where id = #{id}" })
    long updatePlan(Plan plan);


    @Select("<script> " +
            "SELECT * from plan where 1=1 "+
            " <if test='planQuery.sellerId != null'> and seller_id = #{planQuery.sellerId} </if> "+
            " <if test='planQuery.date != null'> and #{planQuery.date} BETWEEN start_date and end_date </if> "+
            " <if test='planQuery.locId != null'> and id in (SELECT plan_id from plan_task where loc_id = #{planQuery.locId}) </if> "+
            " </script>")
    @ResultMap(value = "planMap")
    List<Plan> selectSpecificPlans(@Param("planQuery") PlanQuery planQuery);





    @Select("SELECT id FROM plan where id in ( " +
            "SELECT plan_id from plan_task where id in ( " +
            "SELECT task_id from plan_material where id = #{materialId}))")
    Long selectPlanIdByMaterial(Long materialId);

    @Update({ "update plan set state = 1 where id = #{id}" })
    long passPlan(Long id);

    @Update({ "update plan set state = 2, remark = #{remark} where id = #{id}" })
    long rejectPlan(Long id, String remark);

    @Select("SELECT id FROM plan where id in (SELECT plan_id from plan_task where loc_id = #{locId})")
    List<Long> selectPlanIdByLoc(Long locId);

}
